﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using NSPetroFexService;

namespace PetroFexHost
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceHost m_ServiceHost;
        public MainWindow()
        {
            InitializeComponent();
            m_ServiceHost = new ServiceHost(typeof(NSPetroFexService.PetroFexService));
            NSPetroFexService.PetroFexService.pumpConnected += new EventHandler<PumpEventArgs>(PetroFexService_pumpConnected);
            NSPetroFexService.PetroFexService.pumpDisconnected += new EventHandler<PumpEventArgs>(PetroFexService_pumpDisconnected);
            NSPetroFexService.PetroFexService.pumpUpdated += new EventHandler<PumpEventArgs>(PetroFexService_pumpUpdated);
            NSPetroFexService.PetroFexService.POSTerminalConnected += new EventHandler<POSEventArgs>(PetroFexService_POSTerminalConnected);
            NSPetroFexService.PetroFexService.POSTerminalDisconnected += new EventHandler<POSEventArgs>(PetroFexService_POSTerminalDisconnected);
            NSPetroFexService.PetroFexService.ManagerSystemConnected += new EventHandler(PetroFexService_ManagerSystemConnected);
            NSPetroFexService.PetroFexService.ManagerSystemDisconnected += new EventHandler(PetroFexService_ManagerSystemDisconnected);
            m_ServiceHost.Open();
            logBox.Items.Add("Server Running...");
        }

        void PetroFexService_ManagerSystemDisconnected(object sender, EventArgs e)
        {
            logBox.Items.Add("Manager System Disconnected");
            logBox.SelectedIndex = logBox.Items.Count -  1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }
        void PetroFexService_ManagerSystemConnected(object sender, EventArgs e)
        {
            logBox.Items.Add("New Manager System Connected");
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }

        void PetroFexService_POSTerminalDisconnected(object sender, POSEventArgs e)
        {
            logBox.Items.Add("POS Terminal (ID: " + e.PosId + ") Disconnected");
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }
        void PetroFexService_POSTerminalConnected(object sender, POSEventArgs e)
        {
            logBox.Items.Add("New POS (ID: " + e.PosId + ") Terminal Connected");
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }

        void PetroFexService_pumpUpdated(object sender, PumpEventArgs e)
        {
            logBox.Items.Add("Pump " + e.PumpId + ", " + e.LitresPumped + ", " + e.ThisSale);
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }
        void PetroFexService_pumpDisconnected(object sender, PumpEventArgs e)
        {
            logBox.Items.Add("Pump " + e.PumpId + " Disconnected.");
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }
        void PetroFexService_pumpConnected(object sender, PumpEventArgs e)
        {
            logBox.Items.Add("Pump " + e.PumpId + " Connected.");
            logBox.SelectedIndex = logBox.Items.Count - 1;
            logBox.ScrollIntoView(logBox.SelectedItem);
        }
    }
}
